home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / memory & system tools / tinymeter / source / tinymeter_main / initclean.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  10KB  |  356 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <dos/dosextens.h>
  5. #include <intuition/intuition.h>
  6. #include <intuition/gadgetclass.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/imageclass.h>
  10. #include <intuition/gadgetclass.h>
  11. #include <intuition/cghooks.h>
  12. #include <intuition/icclass.h>
  13. #include <intuition/classes.h>
  14. #include <intuition/sghooks.h>
  15. #include <intuition/screens.h>
  16. #include <datatypes/datatypesclass.h>
  17. #include <datatypes/datatypes.h>
  18. #include <datatypes/pictureclass.h>
  19. #include <graphics/gfxbase.h>
  20. #include <graphics/text.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <utility/tagitem.h>
  23. #include <utility/hooks.h>
  24. #include <string.h>
  25. #include <clib/macros.h>
  26. #include "gaugeclass.h"
  27. #include "tinymeter.h"
  28.  
  29. extern struct IntuitionBase *IntuitionBase;
  30.  
  31. struct Gadget my_drag=
  32. {
  33.     NULL,
  34.     0,1,
  35.     0,0,
  36.     GFLG_GADGHNONE,
  37.     GACT_IMMEDIATE,
  38.     GTYP_SYSGADGET|GTYP_WDRAGGING,
  39.     0,
  40.     0,
  41.     0,
  42.  
  43.     0,
  44.     0,
  45.     2,
  46.     0
  47. };
  48.  
  49. struct Gadget my_close=
  50. {
  51.     NULL,
  52.     0,0,
  53.     16,16,
  54.     NULL,
  55.     GACT_IMMEDIATE,
  56.     GTYP_SYSGADGET|GTYP_CLOSE,
  57.     0,
  58.     0,
  59.     0,
  60.  
  61.     0,
  62.     0,
  63.     1,
  64.     0
  65. };
  66.  
  67. struct Gadget my_size=
  68. {
  69.     NULL,
  70.     0,0,
  71.     16,16,
  72.     GFLG_GADGHNONE,
  73.     GACT_IMMEDIATE,
  74.     GTYP_SYSGADGET|GTYP_SIZING,
  75.     0,
  76.     0,
  77.     0,
  78.  
  79.     0,
  80.     0,
  81.     3,
  82.     0
  83. };
  84.  
  85. new_window_size(struct tm_sys_set *set,struct tm_data *data)
  86. {
  87.     struct Window   *win=data->win;
  88.     my_drag.Width   =win->Width;
  89.     my_drag.Height  =win->Height-17;
  90.     my_size.TopEdge =win->Height-16;
  91.     my_size.LeftEdge=win->Width -16;
  92. }
  93.  
  94.  
  95. struct TextFont *loadFont(char *name, UWORD size)
  96. {
  97.     struct  TextFont     *tf;
  98.     struct  TextAttr      my_text_attr;
  99.     struct  TextAttr      topaz_font =
  100.     {
  101.         "topaz.font",
  102.         8,
  103.         0,
  104.         FPF_ROMFONT,
  105.     };
  106.  
  107.     my_text_attr.ta_Name =(UBYTE *)name;
  108.     my_text_attr.ta_YSize=(UWORD)size;
  109.     my_text_attr.ta_Flags=FPF_DISKFONT | FPB_PROPORTIONAL;
  110.  
  111.     tf=(struct TextFont *)OpenDiskFont((struct TextAttr *)&my_text_attr);
  112.     if(!tf)
  113.     {
  114.         tf=(struct TextFont *)OpenFont(&topaz_font);
  115.         if(!tf) return(0L);
  116.     }
  117.     return(tf);
  118. }
  119.  
  120. struct Window *open_new_window(struct tm_sys_set *set,struct tm_data *data, UWORD ysiz)
  121. {
  122.     struct Window *window;
  123.  
  124.     if((window=(struct Window *)OpenWindowTags(0,
  125.                    WA_Left,            set->x_pos,
  126.                    WA_Top,             set->y_pos,
  127.                    WA_Width,           set->x_siz,
  128.                    WA_MaxWidth,        4096,
  129.                    WA_MaxHeight,       ysiz,
  130.                    WA_MinHeight,       ysiz,
  131.                    WA_Height,          ysiz,
  132.                    WA_MinWidth,        data->min_x_size,
  133.                    WA_Flags,           (set->win_backfront!=win_back ? 0 : WFLG_BACKDROP )|WFLG_BORDERLESS|WFLG_RMBTRAP,
  134.                    WA_PubScreen,       data->scr,
  135.                    TAG_DONE)))
  136.     {
  137.         my_drag.Width=   window->Width;
  138.         my_drag.Height=  window->Height-17;
  139.         my_size.TopEdge= window->Height-16;
  140.         my_size.LeftEdge=window->Width -16;
  141.         if(set->win_move==win_normal)
  142.         {
  143.             AddGadget(window,&my_drag, (UWORD)0);
  144.             AddGadget(window,&my_size, (UWORD)0);
  145.             AddGadget(window,&my_close,(UWORD)0);
  146.         }
  147.         ModifyIDCMP(window,IDCMP_NEWSIZE|IDCMP_CLOSEWINDOW|IDCMP_CHANGEWINDOW|IDCMP_SIZEVERIFY|IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS);
  148.         return(window);
  149.     }
  150.     else return(0L);
  151. }
  152.  
  153. struct Window *snapBackground(struct tm_sys_set *set,struct tm_data *data)
  154. {
  155.     struct Window *win=data->win;
  156.     UWORD x,y,xs,ys;
  157.  
  158.     x=win->LeftEdge;
  159.     y=win->TopEdge;
  160.     xs=win->Width;
  161.     ys=win->Height;
  162.     set->x_pos=x;
  163.     set->y_pos=y;
  164.     set->x_siz=xs;
  165.  
  166.     if(data->bg_bm) FreeBitMap(data->bg_bm);
  167.     if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,data->scr->RastPort.BitMap->Depth,0,0))
  168.     {
  169.         RemoveGList(win,win->FirstGadget,-1);
  170.         CloseWindow(win);
  171.         BltBitMap(data->scr->RastPort.BitMap,x,y,data->bg_bm,0,0,xs,ys,0xc0,0xff,0);
  172.         return((struct Window *)open_new_window(set,data,ys));
  173.     }
  174.     else return(0L);
  175. }
  176.  
  177. void CopyTiledBitMap(struct BitMap *Src,WORD SrcSizeX,WORD SrcSizeY,WORD DstSizeX,WORD DstSizeY ,struct BitMap *Dst)
  178. {
  179.     WORD PosX,
  180.          PosY;
  181.     WORD SizeX,
  182.          SizeY;
  183.     for (PosX = 0,SizeX = MIN(SrcSizeX,DstSizeX);PosX<DstSizeX;)
  184.     {
  185.         for (PosY = 0,SizeY = MIN(SrcSizeY,DstSizeY);PosY<DstSizeY;)
  186.         {
  187.                 BltBitMap(Src,0,0,Dst,PosX,PosY,SizeX,SizeY,0xC0,0xff,0L);
  188.                 PosY += MIN(SizeY,DstSizeY-PosY);
  189.                 SizeY = MIN(SizeY,DstSizeY-PosY);
  190.         }
  191.         PosX += MIN(SizeX,DstSizeX-PosX);
  192.         SizeX = MIN(SizeX,DstSizeX-PosX);
  193.     }
  194. }
  195.  
  196. fileBackground(struct tm_sys_set *set,struct tm_data *data)
  197. {
  198.     struct Window       *win=data->win;
  199.     struct BitMap       *work;
  200.     struct BitMapHeader *header;
  201.     UWORD x,y,xs,ys;
  202.  
  203.     if (data->bg_bm) FreeBitMap(data->bg_bm);
  204.     if (data->dt_object = (Object *)NewDTObject(set->bg_picture,
  205.                                             DTA_SourceType       ,DTST_FILE,
  206.                                             DTA_GroupID          ,GID_PICTURE,
  207.                                             PDTA_Remap           ,TRUE,
  208.                                             PDTA_Screen          ,data->scr,
  209.                                             PDTA_FreeSourceBitMap,TRUE,
  210.                                             OBP_Precision        ,PRECISION_IMAGE,
  211.                                             TAG_DONE))
  212.     {
  213.         if ( DoDTMethod(data->dt_object,NULL,NULL,DTM_PROCLAYOUT,NULL,1))
  214.         {
  215.             GetDTAttrs(data->dt_object,PDTA_BitMapHeader,&header,PDTA_DestBitMap,&work,TAG_DONE);
  216.             if (work==FALSE) GetDTAttrs(data->dt_object,PDTA_BitMap,&work,TAG_DONE);
  217.  
  218.             x=win->LeftEdge;
  219.             y=win->TopEdge;
  220.             xs=win->Width;
  221.             ys=win->Height;
  222.             if(data->bg_bm=(struct BitMap *)AllocBitMap(xs,ys,work->Depth,0,0))
  223.             {
  224.                 CopyTiledBitMap(work,header->bmh_Width,header->bmh_Height,xs,ys,data->bg_bm);
  225.             }
  226.             DisposeDTObject((Object *)data->dt_object);
  227.         }
  228.         else data->bg_bm=0L;
  229.     }
  230.     else data->bg_bm=0L;
  231. }
  232.  
  233. ULONG obtainPen( struct tm_data *data, struct GAU_Color *scr)
  234. {
  235.     if(scr->pen)
  236.     {
  237.         return(scr->red);
  238.     }
  239.     else
  240.     {
  241.         return(ObtainBestPenA(data->scr->ViewPort.ColorMap, scr->red, scr->green, scr->blue, 0L));
  242.     }
  243. }
  244.  
  245. struct Window *openWindow(struct tm_sys_set *set,struct tm_data *data)
  246. {
  247.     UWORD               y_siz,
  248.                         line,
  249.                         i,
  250.                         tmp;
  251.     struct tm_gau_set   *many;
  252.     struct RastPort     *tmpras;
  253.  
  254.     if( data->scr=(struct Screen *)LockPubScreen((char *)&set->pub_name[0]))
  255.     {
  256.         data->on_public=TRUE;
  257.     }
  258.     else
  259.     {
  260.         data->scr=(struct Screen *)IntuitionBase->FirstScreen;
  261.         data->on_public=FALSE;
  262.     }
  263.  
  264.     /* Calc Height of lines, labelpos (starting point of gauges) and open fonts for that... */
  265.  
  266.     data->labelpos=0;
  267.     if(tmpras=(struct RastPort *)AllocVec(sizeof(struct RastPort),MEMF_CLEAR))
  268.     {
  269.         InitRastPort(tmpras);
  270.         for(i=0;i<data->num_of_gaug;i++)data->gauge_y_size[i]=0;
  271.         for(i=0,many=data->list;i<data->num_of_gaug;i++)
  272.         {
  273.             if(many->type!=typ_none)
  274.             {
  275.                 line=(i/(set->colums));
  276.                 if(data->Font[i]=loadFont(many->font,many->font_size))
  277.                     data->gauge_y_size[line]=MAX(data->gauge_y_size[line],(data->Font[i]->tf_YSize*(many->size_y+100))/100);
  278.                 if((many->gauge_type!=typ_histmeter)&&(many->type!=typ_clock_))
  279.                 {
  280.                     SetFont(tmpras,data->Font[i]);
  281.                     tmp=TextLength(tmpras,many->label,my_strlen(many->label));
  282.                     if(tmp>data->labelpos)data->labelpos=tmp;
  283.                 }
  284.             }
  285.             else data->Font[i]=0;
  286.             many=many->next;
  287.         }
  288.         data->labelpos+=4; FreeVec(tmpras);
  289.  
  290.         /* Setup used lines and gauge_x_size */
  291.         data->num_of_rows   =((data->num_of_gaug-1)/set->colums)+1;
  292.         data->min_x_size    =((data->labelpos+8)*set->colums)+(set->win_space_x*(set->colums-1))+(set->win_border_x<<1);
  293.         if(data->min_x_size>set->x_siz)set->x_siz=data->min_x_size;
  294.         data->gauge_x_size  =(set->x_siz-(set->win_border_x<<1)-((set->colums-1)*set->win_space_x))/set->colums;
  295.  
  296.         /* Setup Window Height */
  297.         for(i=0,y_siz=0;i<data->num_of_rows;i++)y_siz+=data->gauge_y_size[i];
  298.         y_siz+=(set->win_border_y<<1)+(set->win_space_y*(data->num_of_rows-1));
  299.  
  300.  
  301.         if(data->win=(struct Window *)open_new_window(set,data,y_siz))
  302.         {
  303.  
  304.             data->bg_color      =obtainPen(data,&set->bg_color);
  305.             data->bright_color  =obtainPen(data,&set->bright_color);
  306.             data->dark_color    =obtainPen(data,&set->dark_color);
  307.             switch (set->bg_type)
  308.             {
  309.                 case    bg_file:
  310.                         fileBackground(set,data);
  311.                         return(data->win);
  312.                         break;
  313.                 case    bg_snap:
  314.                         return(data->win=snapBackground(set,data));
  315.                         break;
  316.                 default:
  317.                         return(data->win);
  318.                         break;
  319.             }
  320.         }
  321.         else {return(0L);}
  322.     }
  323.     else {return(0L);}
  324. }
  325.  
  326. closeWindow(struct tm_sys_set *set,struct tm_data *data)
  327. {
  328.     int i;
  329.  
  330.     if(data->win)
  331.     {
  332.         RemoveGList(data->win,data->win->FirstGadget,-1);
  333.         CloseWindow(data->win);
  334.         data->win=0L;
  335.     }
  336.  
  337.     for(i=0;i<data->num_of_gaug;i++)
  338.         if(data->Font[i])
  339.         {
  340.             CloseFont(data->Font[i]);
  341.             data->Font[i]=0L;
  342.         }
  343.  
  344.     if(data->on_public)
  345.     {
  346.         UnlockPubScreen((char *)&set->pub_name[0]);
  347.         data->on_public=FALSE;
  348.     }
  349.  
  350.     if(data->bg_bm)
  351.     {
  352.         FreeBitMap(data->bg_bm);
  353.         data->bg_bm=0L;
  354.     }
  355. }
  356.